//
//  ASMyHonorVC.m
//  AttendenceSys
//
//  Created by 窦芳俊 on 16/2/29.
//  Copyright © 2016年 窦芳俊. All rights reserved.
//

#import "ASMyHonorVC.h"
#import "ASAddMyHonorVC.h"


@interface ASMyHonorVC ()

@end

@implementation ASMyHonorVC

- (instancetype)initWithAchiTypeId:(NSInteger)typeId typeName:(NSString *)typeName
{
    self = [super init];
    if (self)
    {
        _achiTypeId = typeId;
        _achiTypeName = typeName;
    }
    
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.strNavTitle = @"奖惩";
    [self setRightText:@"增加"];
    
    [self initUI];
    
    [self fetchHonorList];
}

- (void)initUI
{
    UIImageView* bgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    bgView.backgroundColor = [UIColor whiteColor];
    bgView.image = [UIImage imageNamed:@"bg_personal"];
    bgView.userInteractionEnabled = YES;
    [self.view addSubview:bgView];
    
    // infoTableView
    _honorTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    _honorTableView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    _honorTableView.delegate = self;
    _honorTableView.dataSource = self;
    _honorTableView.backgroundColor = [UIColor whiteColor];
    _honorTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _honorTableView.rowHeight = kHonorCellHeight;
    [_honorTableView registerClass:[ASHonorCell class] forCellReuseIdentifier:NSStringFromClass([ASHonorCell class])];
    [bgView addSubview:_honorTableView];
}

- (void)fetchHonorList
{
    //获取用户成果相关数据
    //http://211.65.216.54/getUserCondition.action?userCode=01120603&conditonTypeId=1 -1表示全部，1专业实习，2志愿服务，3社会实践，4其他
    
    NSString* refer = @"getUserCondition.action?";
    NSString* prefix = kASHttpUrl(refer);
    NSString* userCode = [ASAppDelegate sharedAppDelegate].userInfo.userCode;
    NSString* achiTypeId = [NSString stringWithFormat:@"%ld", (long)_achiTypeId];
    NSString* totalUrl = [NSString stringWithFormat:@"%@userCode=%@&conditonTypeId=%@", prefix, userCode, achiTypeId];
    
    [SVProgressHUD showWithStatus:@"获取中..." maskType:SVProgressHUDMaskTypeClear];
    ASNetworkEngine *networkingEngine = [ASNetworkEngine sharedInstance];
    [networkingEngine GET:totalUrl parameters:nil success:^(id responseObject) {
        NSDictionary* jsonRsp = (NSDictionary *)responseObject;
        if (isSuccessRsp(jsonRsp))
        {
            NSArray* data = [jsonRsp objectForKey:kASJsonData];
            
            [SVProgressHUD dismiss];
            
            [self parseData:data];
            [_honorTableView reloadData];
        }
        else
        {
            [SVProgressHUD showErrorWithStatus:errorMsgRsp(jsonRsp)];
        }
    } failure:^(NSError *error) {
        //[SVProgressHUD showErrorWithStatus:error.description];
        [SVProgressHUD showErrorWithStatus:@"网络异常，请稍后再试"];
    }];
}

- (void)parseData:(NSArray*)arr
{
    if (_honorArray == nil)
    {
        _honorArray = [[NSMutableArray alloc] init];
    }
    
    for (NSInteger i = 0; i < [arr count]; i++)
    {
        ASHonorInfo* info = [[ASHonorInfo alloc] init];
        info.honorName = [[arr objectAtIndex:i] objectForKey:@"conditionDesc"];
        //info.honorDepart;
        info.honorLevel = [[arr objectAtIndex:i] objectForKey:@"conditionLevel"];
        info.honorTime = [[arr objectAtIndex:i] objectForKey:@"conditionDate"];
        
        [_honorArray addObject:info];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (void)rightButtonPressed:(id)action
{
    ASAddMyHonorVC *addVC = [[ASAddMyHonorVC alloc] init];
    addVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:addVC animated:YES];
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_honorArray count];
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ASHonorCell class])];
    
    ASHonorCell *hCell = (ASHonorCell *)cell;
    [hCell updateHonorCellStaus:[_honorArray objectAtIndex:[indexPath row]]];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

@end






